package com.abbas.CayTcore.presenter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.FragmentStatePagerAdapter
import com.abbas.CayTcore.R
import com.abbas.CayTcore.base.BasePresenter
import com.abbas.CayTcore.entity.CoreLW
import com.abbas.CayTcore.model.WindingModel
import com.abbas.CayTcore.ui.CoreDetailFragment
import com.abbas.CayTcore.ui.WindingFragment
import com.abbas.CayTcore.view.ViewContract
import q.rorbin.verticaltablayout.adapter.TabAdapter
import q.rorbin.verticaltablayout.widget.ITabView

class WindingPresenter : BasePresenter<ViewContract.WindingView, WindingModel>() {

    fun getCore(lw: CoreLW) {
        view.get()?.showLoading()
        model.getCore(lw, {
            if (!it.isNullOrEmpty()){
                it.apply {
                    view.get()
                        ?.onCoreDataGet(
                            object : FragmentStatePagerAdapter((view.get() as WindingFragment).childFragmentManager,
                                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                                override fun getCount(): Int = this@apply.size

                                override fun getItem(position: Int): Fragment =
                                    CoreDetailFragment(this@apply[position])

                            },
                            object : TabAdapter {
                                override fun getCount(): Int = this@apply.size

                                override fun getBadge(position: Int): ITabView.TabBadge =
                                    ITabView.TabBadge.Builder()
                                        .setBackgroundColor(R.color.gray200)
                                        .build()

                                override fun getIcon(position: Int): ITabView.TabIcon? = null

                                override fun getTitle(position: Int): ITabView.TabTitle =
                                    ITabView.TabTitle.Builder()
                                        .setContent(this@apply[position].getHG())
                                        .setTextColor(R.color.colorPrimary, R.color.gray200)
                                        .build()

                                override fun getBackground(position: Int): Int {
                                    return when (position % 2) {
                                        0 -> R.color.gray200
                                        else -> android.R.color.white
                                    }
                                }
                            }
                        )
                }
            }

            view.get()?.closeLoading()
        }, {
            view.get()?.apply {
                showToast("获取磁芯数据失败")
                closeLoading()
            }
        })
    }

    override fun initModel(): WindingModel {
        return WindingModel()
    }
}